﻿Imports System.Collections.ObjectModel

Public Class RawObjectVm
    Inherits ViewModelBase
    Implements IObjectChangeListener


    Public Sub New(obj As RawObject)

        RawObject = obj
        mTokens = New ObservableCollection(Of TokenBaseVm)
        ReloadTokens()
        ObjectChangeManager.Instance.RegisterForChangeNotice(obj, Me)
    End Sub

    Public Sub ReloadTokens()
        Tokens.Clear()
        For Each Token As Token In RawObject.Tokens
            Dim usage As TokenUsage = TokenDictionary.Instance.GetUsage(Token.Name, RawObject.TypeCode)
            If usage IsNot Nothing Then
                Dim tokenVm As TokenBaseVm = Nothing
                If usage.ArgsType = ArgsType.Standard Then
                    tokenVm = New FixedTokenVm(Token, Me, usage)
                ElseIf usage.ArgsType = ArgsType.List Then
                    tokenVm = New FlexTokenVm(Token, Me, usage)
                Else
                    Throw New InvalidOperationException("ArgsType not handled")
                End If
                Tokens.Add(tokenVm)
            End If
        Next
    End Sub

    Private Sub ObjectChanged(e As ObjectChangeEventArgs) Implements IObjectChangeListener.ObjectChanged
        If e.Obj Is RawObject AndAlso e.Source IsNot Me Then
            RaisePropertyChanged("")
            If e.ReloadTokens Then
                ReloadTokens()
            Else
                For Each t As TokenBaseVm In mTokens
                    t.RefreshArguments()
                    t.RaisePropertyChanged("")
                Next
            End If
        End If
    End Sub

    Private mRawObject As RawObject
    Public Property RawObject As RawObject
        Get
            Return mRawObject
        End Get
        Private Set(value As RawObject)
            If mRawObject IsNot value Then
                mRawObject = value
                RaisePropertyChanged("RawObject")
            End If
        End Set
    End Property

    Private mTokens As ObservableCollection(Of TokenBaseVm)
    Public Property Tokens As ObservableCollection(Of TokenBaseVm)
        Get
            Return mTokens
        End Get
        Set(value As ObservableCollection(Of TokenBaseVm))
            If mTokens IsNot value Then
                mTokens = value
                RaisePropertyChanged("Tokens")
            End If
        End Set
    End Property

    Public ReadOnly Property ObjectId As String
        Get
            Return mRawObject.ObjectId
        End Get
    End Property

    Public Sub NotifyObjectChanged(reloadTokens As Boolean)
        ObjectChangeManager.Instance.NotifyObjectChanged(RawObject, reloadTokens, Me)
    End Sub

    Public Function InsertToken(index As Integer, token As Token, notify As Boolean) As TokenBaseVm
        Dim vm As TokenBaseVm = New FixedTokenVm(token, Me)
        Me.Tokens.Insert(0, vm)
        Me.RawObject.Tokens.Insert(0, token)
        If notify Then
            NotifyObjectChanged(True)
        End If
        Return vm
    End Function

    Public Sub RemoveToken(index As Integer, notify As Boolean)
        Me.Tokens.RemoveAt(index)
        Me.RawObject.Tokens.RemoveAt(index)
        If notify Then
            NotifyObjectChanged(True)
        End If
    End Sub

End Class